package com.example.andylau96.res.Function.Account;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.andylau96.res.Main.MainActivity;
import com.example.andylau96.res.R;
import com.example.andylau96.res.DataBase.UserDataBase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AndyLau96 on 2016/12/10.
 */
public class bDelete extends Fragment implements AdapterView.OnItemClickListener,View.OnClickListener
{
    private LinearLayout title;
    private ListView listview;
    private EditText idtext,chartext;
    private Button idchaxun,charchaxun;
    private ImageButton re;
    private SQLiteDatabase db;
    private ArrayAdapter adapter;
    private TextView et;
    private List<String> list=new ArrayList<String>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.ychange,container,false);
        title= (LinearLayout) view.findViewById(R.id.ychange_title);
        listview= (ListView) view.findViewById(R.id.ychange_list);
        idtext= (EditText) view.findViewById(R.id.ychange_idedit);
        chartext= (EditText) view.findViewById(R.id.ychange_charedit);
        idchaxun= (Button) view.findViewById(R.id.ychange_idchaxun);
        charchaxun= (Button) view.findViewById(R.id.ychange_charchaxun);
        re= (ImageButton) view.findViewById(R.id.ychange_title_return);

        idchaxun.setOnClickListener(this);
        charchaxun.setOnClickListener(this);
        listview.setOnItemClickListener(this);
        re.setOnClickListener(this);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        db=new UserDataBase(getActivity().getApplicationContext()).getWritableDatabase();
        list=new ArrayList<String>();
        switch(id)
        {
            case R.id.ychange_idchaxun:
                int t=Integer.valueOf(idtext.getText().toString());
                Cursor cr=db.rawQuery("select *from worker where WorkId="+t,null);
                if(cr.getCount()==0)
                {
                    idtext.setText("");
                    Toast.makeText(getActivity(),"不存在此工号！",Toast.LENGTH_SHORT).show();
                }
                else{
                    cr.moveToFirst();
                    int num=cr.getInt(cr.getColumnIndex("WorkId"));
                    String name=cr.getString(cr.getColumnIndex("Name"));
                    int age=cr.getInt(cr.getColumnIndex("Age"));
                    String tel=cr.getString(cr.getColumnIndex("Tel"));
                    list.add("工号\t\t姓名\t\t年龄\t\t电话");
                    list.add(num+"   "+name+"   "+age+"   "+tel);
                    adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
                    listview.setAdapter(adapter);
                }
                break;
            case R.id.ychange_charchaxun:
                Cursor cr2=db.rawQuery("select *from worker where Name like'%"+chartext.getText().toString()+"%'",null);
                if(cr2.getCount()==0)
                {
                    chartext.setText("");
                    Toast.makeText(getActivity(),"不存在此字段的信息！",Toast.LENGTH_SHORT).show();
                }
                else{
                    list.add("工号\t\t姓名\t\t年龄\t\t电话");
                    cr2.moveToFirst();
                    while(!cr2.isAfterLast())
                    {
                        int num = cr2.getInt(cr2.getColumnIndex("WorkId"));
                        String name = cr2.getString(cr2.getColumnIndex("Name"));
                        int age = cr2.getInt(cr2.getColumnIndex("Age"));
                        String tel = cr2.getString(cr2.getColumnIndex("Tel"));
                        list.add(num+"   "+name+"   "+age+"   "+tel);
                        cr2.moveToNext();
                    }
                    adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
                    listview.setAdapter(adapter);
                }
                break;
            case R.id.ychange_title_return:
                getFragmentManager().popBackStack();

        }
        db.close();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        et=(TextView)view;
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("确定删除吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                db=new UserDataBase(getActivity().getApplicationContext()).getWritableDatabase();
                String t=et.getText().toString();
                String s[]=t.split("   ");
                int num=Integer.valueOf(s[0]);
                db.execSQL("delete from worker where WorkId="+num);
                db.execSQL("delete from user where WorkId="+num);
                Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                list=new ArrayList<String>();
                adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
                listview.setAdapter(adapter);
                dialogInterface.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }
}
